# _*_ coding:utf-8 _*_
# @Time      : 9:00
# @Author    :baizhou
import uuid

import cv2
import numpy as np
import matplotlib.pyplot as plt
from django.conf import settings
import os


def show_gray(path):
    gray_img = cv2.imread(path,0)
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, gray_img)
    return file_name


def show_Rcomponent(path):
    '''
    显示R分量
    :param path:
    :return:
    '''
    img = cv2.imread(path)
    b, g, r = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    r_component = cv2.merge([zeros, zeros, r])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, r_component)
    return file_name


def show_Gcomponent(path):
    img = cv2.imread(path)
    b, g, r = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    g_component = cv2.merge([zeros, g, zeros])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, g_component)
    return file_name


def show_Bcomponent(path):
    img = cv2.imread(path)
    b, g, r = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    b_component = cv2.merge([b, zeros, zeros])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, b_component)
    return file_name



def show_Hcomponent(path):
    img = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    h_component = cv2.merge([h, zeros, zeros])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, h_component)
    return h_component

def show_Scomponent(path):
    img = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    s_component = cv2.merge([zeros, s, zeros])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, s_component)
    return s_component



def show_Vcomponent(path):
    img = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(img)
    zeros = np.zeros(img.shape[:2], dtype="uint8")
    v_component = cv2.merge([zeros, zeros, v])
    # 保存静态文件的路径
    upload_dir = os.path.join(settings.DEAL_DIR)
    suffix_file = path.split(".")[-1]
    # 文件名
    file_name = str(uuid.uuid1()) + "." + suffix_file
    save_path = os.path.join(upload_dir, file_name)
    cv2.imwrite(save_path, v_component)
    return v_component





# if __name__ == '__main__':
#     res = show_Rcomponent(r"E:\ImageWork\machine\image_deal\static\upload\b1073b92-4583-11e9-92da-e86a6420dfa7.png")
#     plt.imshow(res["Rcomponent"])
